//
//  ForgetPsdViewController.m
//  Kfangchan
//
//  Created by 朋飞 on 14-11-13.
//  Copyright (c) 2014年 Beijing Renjuren Network Technology Co., Ltd. All rights reserved.
//

#import "ForgetPsdViewController.h"
#pragma mark 忘记密码
#pragma mark
@interface ForgetPsdViewController ()
@property (strong, nonatomic) IBOutlet UIButton *confirmBtn;
@property (strong, nonatomic) IBOutlet UIButton *getmsgBtn;
@property (strong, nonatomic) IBOutlet UITextField * phoneTextField;
@property (strong, nonatomic) IBOutlet UITextField * msgcodeTextField;
@property (strong, nonatomic) IBOutlet UITextField * psdTextField;
@property (strong, nonatomic) IBOutlet UITextField * confirmpsdTextField;




@end

@implementation ForgetPsdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 返回
    self.navigationItem.titleView = [[Theam currentTheam] navigationTitleViewWithTitle:@"忘记密码"];
    self.navigationItem.leftBarButtonItem = [[Theam currentTheam] navigationBarLeftButtonItemWithImage:[UIImage imageNamed:@"nav_back.png"] Target:self Selector:@selector(back)];
     [self.getmsgBtn.layer setCornerRadius:5.0];
}


#pragma mark 返回
#pragma mark
- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark  获得短信验证码
#pragma mark
-(IBAction) getMsgcode:(id)sender{
    BOOL verify = [self verify:self.phoneTextField.text];
    if(verify){
        // 构建请求参数
        NSDictionary *param = @{
                                @"mobile":self.phoneTextField.text
                                };
        // 请求
        MKNetworkOperation *operation = [[BaseEngine sharedEngine]RunRequest:[param mutableCopy] path:KFC_VERIFY_PHONE completionHandler:^(id responseObject) {
            if(responseObject){
                [self startTime60];
            }
            
        } errorHandler:^(NSError *error){
            showCustomAlertMessage(@"网路错误");
        }finishHandler:^(id responseObject) {
                        
        }];
        
        [self.opHandlers addObject:operation];
        
    }
}


#pragma mark 确认
#pragma mark
-(IBAction)confirm:(id)sender{
    BOOL verify  = [self verify];
    if(verify){
        //弱引用
        __weak ForgetPsdViewController *weak_self = self;
        // 构建请求参数
        NSDictionary *param = @{
                                @"mobile":self.phoneTextField.text,
                                @"vericode":self.msgcodeTextField.text,
                                @"passwd":self.psdTextField.text
                                };
        // 请求
        MKNetworkOperation *operation = [[BaseEngine sharedEngine]RunRequest:[param mutableCopy] path:KFC_FORGET_PSD completionHandler:^(id responseObject) {
            
            [weak_self back];
            
        } errorHandler:^(NSError *error){
            
            showCustomAlertMessage(@"网路错误");
            
        }finishHandler:^(id responseObject) {
            
        }];
        
        [self.opHandlers addObject:operation];
        
    }
    

}

- (BOOL) verify {
    NSString * confrimpsd = self.confirmpsdTextField.text;
    NSString * phoneNum = self.phoneTextField.text;
    NSString * msgCode = self.msgcodeTextField.text;
    NSString * psd = self.psdTextField.text;
    //  NSString * requestCode = self.requestcodeTextField.text;
    UIAlertView * alert = nil;
   
    
    if ( !validateMobile(phoneNum) ) {
        alert = [[UIAlertView alloc] initWithTitle:nil message:@"请输入正确的手机号" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return NO;
    }
    
    if(msgCode.length!=4){
        alert = [[UIAlertView alloc]initWithTitle:nil message:@"请输入正确的短信验证码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return NO;
    }
    
    if(psd.length==0){
        alert = [[UIAlertView alloc]initWithTitle:nil message:@"请输入密码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return NO;
    }
    
    //    if(password.length<6){
    //        alert = [[UIAlertView alloc]initWithTitle:nil message:@"用户名不少于6个字符" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    //        [alert show];
    //        return NO;
    //    }
    //
    
    if (![psd isEqualToString:confrimpsd]) {
        alert = [[UIAlertView alloc] initWithTitle:nil message:@"请确保密码一致" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return NO;
    }
    
    
    
    return YES;
}


- (BOOL) verify: (NSString *) phone {
    
    UIAlertView * alert = nil;
    
    if ( !validateMobile(phone) ) {
        alert = [[UIAlertView alloc] initWithTitle:nil message:@"请输入正确的手机号" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return NO;
    }
    
    return YES;
    
}


#pragma mark -
#pragma mark 关闭键盘
- (IBAction)hideKeyboard {
    [self.psdTextField resignFirstResponder];
    [self.phoneTextField resignFirstResponder];
    [self.msgcodeTextField resignFirstResponder];
    [self.confirmpsdTextField resignFirstResponder];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
-(void)startTime60{
    
    __block int timeout=60; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self.getmsgBtn.layer setCornerRadius:5.0];
                self.getmsgBtn.backgroundColor =[UIColor colorWithHexString:@"66b97a"];
                [self.getmsgBtn setTitle:@"重新发送" forState:UIControlStateNormal];
                self.getmsgBtn.userInteractionEnabled = YES;
            });
        }else{
            //            int minutes = timeout / 60;
            
            int seconds = timeout % 61;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self.getmsgBtn.layer setCornerRadius:5.0];
                self.getmsgBtn.backgroundColor =[UIColor colorWithHexString:@"949494"];
                [self.getmsgBtn setTitle:[NSString stringWithFormat:@"%@秒后重新发送",strTime] forState:UIControlStateNormal];
                self.getmsgBtn.userInteractionEnabled = NO;
                
                
            });
            timeout--;
            
        }
    });
    dispatch_resume(_timer);
    
}


@end
